//
//  SettingViewController.m
//  MiaoTu
//
//  Created by dllo on 16/9/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SettingViewController.h"
#import "UIView+Frame.h"
#import "Masonry.h"

#define kSettingTableCell @"SettingTableCellIdentifier"

@interface SettingViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<NSArray *> *dataArray;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    [self createData];
}

- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kSettingTableCell];
    [self createTableFooterView];
}

- (void)createTableFooterView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tableView.dk_width, 100)];
    UIButton *exitLogin = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:exitLogin];
    [exitLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(view.mas_top).offset(30);
        make.left.mas_equalTo(view.mas_left).offset(10);
        make.right.mas_equalTo(view.mas_right).offset(-10);
        make.height.mas_equalTo(40);
    }];
    exitLogin.backgroundColor = [UIColor colorWithRed:0.894 green:0.408 blue:0.043 alpha:1.000];
    [exitLogin setTitle:@"退出登录" forState:UIControlStateNormal];
    [exitLogin setTintColor:[UIColor whiteColor]];
    [exitLogin addTarget:self action:@selector(exitLogin) forControlEvents:UIControlEventTouchUpInside];
    [_tableView beginUpdates];
    _tableView.tableFooterView = view;
    [_tableView endUpdates];
}

- (void)createData {
    self.dataArray = @[@[@"去评分"], @[@"新消息通知提醒", @"黑名单", @"清除缓存"], @[@"联系妙途", @"帮助与反馈"]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSettingTableCell forIndexPath:indexPath];
    cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}

- (void)exitLogin {
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self.navigationController popToRootViewControllerAnimated:YES];
    self.tabBarController.selectedIndex = 0;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
